//
#include "Protect.h"
//
//*****************************************************************************
//!\brief	The Locked Rotor Current Protection and the Locked Rotor Phase Protection control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void LRCP_Ctrl(void)
{
	//------Check Protect --------------
	if(!protect.bitprt.lrcp &&  !protect.bitprt.lrpp)
	{
		if((g_Stage == SECOND_STAGE) && (INDEX_FOC != LOCATION))
		{
			if(flag.check_sector_ok)
			{
				flag.check_sector_ok = 0;
				//
				delay.lrcp = 0;
				g_CntLRCP = 0;
				timeout.lrcp = 0;
				//
				delay.lrpp = 0;
				g_CntLRPP = 0;
				timeout.lrpp = 0;
			}
			else
			{
				if(g_CurrIBusAVG >= LRCP_IBUS_ADC)      // Locked Rotor Current Protection
				{
					delay.lrcp = 1;
					if(timeout.lrcp)
					{
						delay.lrcp = 0;
						g_CntLRCP = 0;
						timeout.lrcp = 0;
						//
						EnterNoneStage();               // Enter into the NONE stage !!!
						//
						protect.bitprt.lrcp = 1;
						//
						#if DEBUG_INFOR_OUTPUT
						printf("LRCP occurred!!\r\n");
						#endif
					}
				}
				else
				{
					delay.lrcp = 0;
					g_CntLRCP = 0;
					timeout.lrcp = 0;
					//
					delay.lrpp = 1;
					if(timeout.lrpp)                    // Locked Rotor Phase Protection
					{
						delay.lrpp = 0;
						g_CntLRPP = 0;
						timeout.lrpp = 0;
						//
						EnterNoneStage();               // Enter into the NONE stage !!!
						//
						protect.bitprt.lrpp = 1;
						//
						#if DEBUG_INFOR_OUTPUT
						printf("LRPP occurred!!\r\n");
						#endif
					}
				}
			}
		}
		else
		{
			delay.lrcp = 0;
			g_CntLRCP = 0;
			timeout.lrcp = 0;
			//
			delay.lrpp = 0;
			g_CntLRPP = 0;
			timeout.lrpp = 0;
		}
	}	
}

//*****************************************************************************
//!\brief	The Over Currrent Protect control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void OCP_Ctrl(void)
{
	//------Check Protect --------------
	if(!protect.bitprt.ocp)
	{
		if(g_CurrIBusAVG >= OCP_IBUS_ADC_LEVEL)       // the OCP current
		{
			delay.ocp = 1;
			g_DelayTimeOCP = OCP_TIME;
			if(timeout.ocp)
			{
				delay.ocp = 0;
				g_CntOCP = 0;
				timeout.ocp = 0;
				//
				EnterNoneStage();                       // Enter into the NONE stage !!!
				//
				protect.bitprt.ocp = 1;	
				//
				#if DEBUG_INFOR_OUTPUT
				printf("OCP_SW occurred!!\r\n");
				#endif
			}
		}
		else
		{
			delay.ocp = 0;
			g_CntOCP = 0;
			timeout.ocp = 0;
		}
	}	
}

//*****************************************************************************
//!\brief	The Under Voltage Protect control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void UVP_Ctrl(void)
{
	//------Check Protect --------------
	if(!protect.bitprt.uvp)
	{
		if(g_VoltageMotor <= VM_MIN_ADC)		// the VM minimum
		{
			delay.uvp = 1;
			g_DelayTimeUVP = UVP_TIME;
			if(timeout.uvp)
			{
				delay.uvp = 0;
				g_CntUVP = 0;
				timeout.uvp = 0;
				//
				EnterNoneStage();				// Enter into the NONE stage !!!
				//
				protect.bitprt.uvp = 1;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("UVP occurred!!\r\n");
				#endif
			}
		}
		else
		{
			delay.uvp = 0;
			g_CntUVP = 0;
			timeout.uvp = 0;
		}

	}
}

//*****************************************************************************
//!\brief	The Over Voltage Protect control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void OVP_Ctrl(void)
{
	//------Check Protect --------------
	if(!protect.bitprt.ovp)
	{
		if(g_VoltageMotor >= VM_MAX_ADC)	// the VM maximum
		{
			delay.ovp = 1;
			g_DelayTimeOVP = OVP_TIME;
			if(timeout.ovp)
			{
				delay.ovp = 0;
				g_CntOVP = 0;
				timeout.ovp = 0;
				//
				EnterNoneStage();				// Enter into the NONE stage !!!
				//
				protect.bitprt.ovp = 1;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("OVP occurred!!\r\n");
				#endif
			}
		}
		else
		{
			delay.ovp = 0;
			g_CntOVP = 0;
			timeout.ovp = 0;
		}

	}
}

//*****************************************************************************
//!\brief	The MOS Over Temperature Protect control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void OTP_Ctrl(void)
{
	//------Check Protect --------------
	if(!protect.bitprt.otp)
	{
		if(g_VoltageMosTemp <= OTP_ADC)
		{
			delay.otp = 1;
			g_DelayTimeOTP = OTP_TIME;
			if(timeout.otp)
			{
				delay.otp = 0;
				g_CntOTP = 0;
				timeout.otp = 0;
				//
				EnterNoneStage();							// Enter into the NONE stage !!!
				//
				protect.bitprt.otp = 1;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("OTP occurred!!\r\n");
				#endif
			}
		}
		else
		{
			delay.otp = 0;
			g_CntOTP = 0;
			timeout.otp = 0;
		}
	}
	else
	{
		if(g_VoltageMosTemp >= OTP_REC_ADC)
		{
			delay.otp = 1;
			g_DelayTimeOTP = OTP_REC_TIME;
			if(timeout.otp)
			{
				delay.otp = 0;
				g_CntOTP = 0;
				timeout.otp = 0;
				//
				protect.bitprt.otp = 0;							// recover
				//
				flag.SysOnOff = 0;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("OTP recover!!\r\n");
				#endif
			}
		}
		else
		{
			delay.otp = 0;
			g_CntOTP = 0;
			timeout.otp = 0;
		}
	}
}
//*****************************************************************************
//!\brief	The AC Protect control logic 
//!\param	None
//!\retval  None                                                                   
//*****************************************************************************
void ACP_Ctrl(void)
{
    //------Check Protect --------------
	if(!protect.bitprt.acp)
	{
		if(!ACP_GPIO)	
		{
			delay.acp = 1;
			g_DelayTimeACP = ACP_TIME;
			if(timeout.acp)
			{
				delay.acp = 0;
				g_CntACP = 0;
				timeout.acp = 0;
				//
				EnterNoneStage();				// Enter into the NONE stage !!!
				//
				protect.bitprt.acp = 1;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("ACP occurred!!\r\n");
				#endif
			}
		}
		else
		{
			delay.acp = 0;
			g_CntACP = 0;
			timeout.acp = 0;
		}

	}
    else
    {
        if(ACP_GPIO)	
		{
			delay.acp = 1;
			g_DelayTimeACP = ACP_TIME;
			if(timeout.acp)
			{
				delay.acp = 0;
				g_CntACP = 0;
				timeout.acp = 0;
				//
				protect.bitprt.acp = 0;
				//
				#if DEBUG_INFOR_OUTPUT
				printf("ACP recover!!\r\n");
				#endif
			}
		}
		else
		{
			delay.acp = 0;
			g_CntACP = 0;
			timeout.acp = 0;
		}
    }
}
//=========================================================================================

